﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PKURG.CostSplit.Business;
using System.Drawing;

public partial class Pages_StageSplit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadAccountList();
            tvAccountList.Nodes[0].ChildNodes[0].Select();
            BindContractList();
            //BindContractList();
        }
    }
    //private void InitData()
    //{
    //    string stageIdString = Request["id"];
    //    if (string.IsNullOrEmpty(stageIdString))
    //    {
    //        Common.GoErrorPage();
    //        return;
    //    }
    //    var info = Dev_Stage.GetInfo(stageIdString);
    //    if (info == null)
    //    {
    //        Common.GoErrorPage();
    //        return;
    //    }

    //    lbTitle.Text = string.Format("{0} 第{1}期", info.ProjectName, info.StageName);


    //}

    private void BindContractList()
    {
        string stageIdString = Request["id"];
        if (string.IsNullOrEmpty(stageIdString))
        {
            Common.GoErrorPage();
            return;
        }
        var info = Dev_Stage.GetInfo(stageIdString);
        if (info == null)
        {
            Common.GoErrorPage();
            return;
        }

        lbTitle.Text = string.Format("{0} {1}", info.ProjectName, info.StageName);

        string erpStageIdString = info.RightId;

        if (tvAccountList.SelectedNode == null)
        {
            gvContractList.Visible = false;
        }
        else
        {
            gvContractList.Visible = true;
            string accountName = "V1." + tvAccountList.SelectedNode.Value;
            List<dynamic> infos = ContractCost.GetContractCostList(erpStageIdString, accountName);
            infos = infos.Where(x => !string.IsNullOrEmpty(x.ContractNum)).ToList();
            gvContractList.DataSource = infos;
            gvContractList.DataBind();

            hl_url.NavigateUrl = string.Format("ViewAccountUnitCost.aspx?id={0}&aid={1}", stageIdString, HttpUtility.UrlEncode(accountName));
        }


    }
    private void LoadAccountList()
    {
        IList<PKURG.CostSplit.Entities.ErpAccount> accountInfos = ERPAccount.GetAllAccount();
        var parentInfos = accountInfos.Where(x => x.Pid == 0).OrderBy(x => x.Name).ToList();
        foreach (var item in parentInfos)
        {
            TreeNode node = new TreeNode(GetItemShowName(item), item.Id.ToString());
            tvAccountList.Nodes.Add(node);
            node.SelectAction = TreeNodeSelectAction.Expand;

            BindTreeItem(accountInfos, node);
        }
    }

    private void BindTreeItem(IList<PKURG.CostSplit.Entities.ErpAccount> accountInfos, TreeNode node)
    {
        var infos = accountInfos.Where(x => x.Pid == int.Parse(node.Value)).OrderBy(x => x.Name).ToList();
        foreach (var item in infos)
        {
            TreeNode childNode = new TreeNode(GetItemShowName(item), item.Id.ToString());
            node.ChildNodes.Add(childNode);
            if (item.IsLeaf == 0)
            {
                BindTreeItem(accountInfos, childNode);
                childNode.SelectAction = TreeNodeSelectAction.Expand;
            }
            else
            {
                childNode.Value = item.Name;
            }
        }
    }

    private string GetItemShowName(PKURG.CostSplit.Entities.ErpAccount item)
    {
        return string.Format("{1}", item.Name, item.Des);
    }

    protected void tvAccountList_SelectedNodeChanged(object sender, EventArgs e)
    {
        BindContractList();
        //string content="<script language = 'javascript'>alert('d');Fixed();</script>";
        //ScriptManager.RegisterStartupScript(this, typeof(Page), "ss", content, true);

    }
    protected void gvContractList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvContractList.PageIndex = e.NewPageIndex;
        BindContractList();

    }
    protected void gvContractList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            dynamic obj = e.Row.DataItem as dynamic;
            if (obj.SyncType == "1")
            {
                e.Row.ForeColor = Color.Green;
            }
            else
            {
                e.Row.ForeColor = Color.Black;
            }
        }

    }
}